﻿using System;
using LitJson;
using UnityEngine;
using UnityEngine.UI;

namespace Script.UI
{
    public enum ShowBtnType
    {
        Two,
        One,
    }

    public enum BtnType
    {
        Left,
        Right,
        Middle
    }
    public class DialogWnd : UIBase
    {
        public Text title;
        public Text content;

        public Button left;
        public Button right;
        public Button middle;

        public Action<BtnType> callback;

        protected override void OnAwake()
        {
            left.onClick.AddListener(OnLeftBtnClick);
            right.onClick.AddListener(OnRightBtnClick);
            middle.onClick.AddListener(OnMiddleBtnClick);
        }

        public static DialogWnd GetWnd()
        {
            var wnd = GameObject.Instantiate(Resources.Load<GameObject>("Panel/DialogWnd"));
            var comp = wnd.GetComponent<DialogWnd>();
            return comp;
        }


        public void OnLeftBtnClick()
        {
            OnBtnClick(BtnType.Left);
        }

        public void OnRightBtnClick()
        {
            OnBtnClick(BtnType.Right);
        }

        public void OnMiddleBtnClick()
        {
            OnBtnClick(BtnType.Middle);
        }

        public void OnBtnClick(BtnType btnType)
        {
            this.callback?.Invoke(btnType);
            DestroyImmediate(gameObject);
            this.callback = null;
        }

        protected override void OnRefresh()
        {
            var data = _data as JsonData;
            SetTitle(data["title"].ToString());
            SetTitle(data["content"].ToString());
            
        }
        
        public void SetBtnType(ShowBtnType btnType)
        {
            switch (btnType)
            {
                case ShowBtnType.Two:
                    left.gameObject.SetActive(true);
                    right.gameObject.SetActive(true);
                    middle.gameObject.SetActive(false);
                    break;
                case ShowBtnType.One:
                    left.gameObject.SetActive(false);
                    right.gameObject.SetActive(false);
                    middle.gameObject.SetActive(true);
                    break;
                default:
                    throw new ArgumentOutOfRangeException(nameof(btnType), btnType, null);
            }
        }

        public void SetTitle(string text)
        {
            title.text = text;
        }

        public void SetContent(string text)
        {
            content.text = text;
        }

        public void SetCallback(Action<BtnType> callback)
        {
            this.callback = callback;
        }
        
    }
}